<?php

/*
    File: index.php
    Author: admin@weixen.com
    Date: 2013.1.8
    Usage: 微信公众平之台有道词典中英文互译
    论坛微信QQ群: 39162035
 */

define("DEBUG", true);
define("TOKEN", "fanyitest");
define("KEYFROM", "weixen");
define("APIKEY", 791576090);

require_once(dirname(__FILE__) . "/wechat.php");

$w = new Wechat(TOKEN, DEBUG);

//首次验证，验证过以后可以删掉
if (isset($_GET['echostr'])) {
    $w->valid();
    exit();
}

//回复用户
$w->reply("reply_cb");

//后续必要的处理...
/* TODO */
exit();

function fanyi($yuanwen,$keyfrom,$apikey)
 {
    	
    	
    $yuanwen=get_utf8_string($yuanwen);
    $doc = new DOMDocument();
    $doc->load("http://fanyi.youdao.com/openapi.do?keyfrom=".$keyfrom."&key=".$apikey."&type=data&doctype=xml&version=1.1&q=".$yuanwen); //读取xml文件
  	$youdaos = $doc->getElementsByTagName( "youdao-fanyi" ); //取得humans标签的对象数组
 foreach( $youdaos as $youdao )
  {
    $paragraphs = $youdao->getElementsByTagName( "paragraph" ); //取得name的标签的对象数组
    $paragraph = $paragraphs->item(0)->nodeValue; //取得node中的值，如<name> </name>
    $paragraph=get_utf8_string($paragraph);
    $exs = $youdao->getElementsByTagName( "ex" ); //取得name的标签的对象数组
    $ex = $exs->item(0)->nodeValue; //取得node中的值，如<name> </name>
    $ex=get_utf8_string($ex);

  }
if($paragraph==$ex){
    return $paragraph;
  }else{
	  return $paragraph.";  ".$ex ;
  }
}


function get_utf8_string($content) {    
	//  将一些字符转化成utf8格式   
	 $encoding = mb_detect_encoding($content, array('ASCII','UTF-8','GB2312','GBK','BIG5'));  
	   return  mb_convert_encoding($content, 'utf-8', $encoding);
	   }
	   
function reply_cb($request, $w)
{
    if ($w->get_msg_type() == "location") {
        return sprintf("你的位置：(%s, %s), 地址：%s",
                $request['Location_X'], $request['Location_Y'], $request['Label']);
    }
    else if ($w->get_msg_type() == "image") { //echo back url
        $PicUrl = $request['PicUrl'];
        return "图片url：" . $PicUrl;
    }
    //else: Text

    $content = trim($request['Content']);
    if ($content === "hello" or $content === "hi" or $content === "你好"or $content === "好") { //貌似第一次加入会发送这个
        return "兄弟，你是幼儿园毕业的么！这么简单的word！！微信论坛欢迎您(www.weixen.com)";
    }

    if ($content !== "url") //发纯文本
    {
        //$w->set_funcflag(); //如果有必要的话，加星标，方便在web处理
        if(!empty($content)){
          $content = fanyi($content,KEYFROM,APIKEY);
            return "小译: " . $content;
          }
        else
            return "请说点什么...";
    }
    else //发图文消息
    {
        //* 单个图文
        return array(
            "title" =>  "hello",
            "description" =>  "world",
            "pic" =>  "http://www.weixen.com/template/eis_d25_031/img/logo.png",
            "url" =>  "http://www.weixen.com",
        );
        // */
        /* 多个图文，并加星标
        $w->set_funcflag();
        return array(
            array(
                "title" =>  "a1",
                "description" =>  "a1",
                "pic" =>  "http://www.weixen.com/template/eis_d25_031/img/logo.png",
                "url" =>  "http://www.weixen.com",
            ),
            array(
                "title" =>  "a2",
                "description" =>  "a2",
                "pic" =>  "http://www.weixen.com/template/eis_d25_031/img/logo.png",
                "url" =>  "http://www.weixen.com",
            ),
        );
        // */
    }
}

?>
